import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;



public class videoSummarize {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * UI part
		 */
//		//Show the coordinate graph
//		final int WINDOW_WIDTH = 1024;
//		final int WINDOW_HEIGHT = 768;
//		JFrame frame = new JFrame();
//		//GraphPanel panel = new GraphPanel(WINDOW_WIDTH, WINDOW_HEIGHT);		
//		MotionPanel motionPanel = new MotionPanel(WINDOW_WIDTH, WINDOW_HEIGHT);
//		
//		frame.getContentPane().add(motionPanel);
//		frame.pack();
//		frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
//		frame.setLocationRelativeTo(null);
//		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		frame.setVisible(true);
		
		/*
		 * Core algorithm part
		 */
		if(args.length < 3)
		{
			System.out.println("Please type video and audio file names and a percentage");
			return;
		}
		//Parameters entered
		String videoName = args[0];
		String audioName = args[1];
		double percent = Double.parseDouble(args[2]);
		
		VideoFile videoFile = new VideoFile(videoName);
		AudioFile audioFile = new AudioFile(audioName);
		//VideoFile videoFile = new VideoFile("terminator.576v");
		//AudioFile audioFile = new AudioFile("terminator.wav");
		audioFile.printInfo();

		ColorParser colorParser = new ColorParser(videoFile, new SignGrayCode());
		colorParser.parse(null);
		VideoSegment[] segments = colorParser.doSegmentation();
		colorParser.genKeyFrames(segments);
		
		AudioParser audioParser = new AudioParser(audioFile);
		audioParser.parse(segments);
		
		FrameMotionParser motionParser = new FrameMotionParser(videoFile);
		motionParser.parse(segments);
		
		//motionPanel.setMotionInfos(fmp.getFrameMotionInfos());
		//motionPanel.setMotionSegments(shots);
		
		for(int i=0; i<segments.length; i++){
			segments[i].calcTotalWeight();
		}
		
//		//Print segments infos
//		System.out.println("****************************************************************************");
//		System.out.println("Segment number: " + segments.length);
//		for(int i=0; i<segments.length; ++i){
//			System.out.println(segments[i]);
//		}
		
		//Clear all parser data, release memory
		colorParser = null;
		audioParser = null;
		motionParser = null;
		System.gc();
		
//		while(true){
			Arrays.sort(segments, new WeightComparator());
			int outputFrames = (int) (videoFile.getTotalFrameNumber() * percent);
			int count = 0;
			int index = 0;
			while(count < outputFrames){
				count += segments[index].getLength();
				++index;
			}
			
			VideoSegment[] outputSegs = new VideoSegment[index];
			for(int i=0; i<outputSegs.length; ++i){
				outputSegs[i] = segments[i];
			}
			Arrays.sort(outputSegs, new StartComparator());
			
			//Print segments infos
			System.out.println("****************************************************************************");
			System.out.println("Output segment number: " + outputSegs.length);
			for(int i=0; i<outputSegs.length; ++i){
				System.out.println(outputSegs[i]);
			}
		
			SummaryGenerator generator = new SummaryGenerator(videoName, audioName);
			generator.generateFiles(outputSegs);
			
//			BufferedReader reader; //specify the reader variable //to be a standard input buffer 
//			reader = new BufferedReader(new InputStreamReader(System.in)); //ask the user for their name 
//			System.out.print("Please input percentage: "); 
//			//read the data entered by the user using 
//			//the readLine() method of the BufferedReader class 
//			//and store the value in the name variable 
//			try {
//				String percentage = reader.readLine();
//				percent = Double.parseDouble(percentage);
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
	}
}
